﻿namespace ThingsGateway.Foundation
{
    public class ReadWriteDevicesTcpClient : ReadWriteDevicesTcpClientBase, IReadWriteDevice
    {
        #region Public Methods

        public override Result<byte[]> Read(string address, ushort length)
        {
            ReadWriteRequest readWriteRequest = new ReadWriteRequest()
            {
                ReadWriteType = ReadWriteType.ReadBytes,
                DeviceAddress = new string[] { address },
                Length = new ushort[] { length },
            };
            try
            {
                try
                {
                    this.Connect(ConnectTimeOut);
                }
                catch (Exception)
                {
                    return new Result<byte[]>(ThingsGateway.Foundation.Resources.Resource.ConnectTimeOut);
                }

                var result = this.GetWaitingClient().SendThenResponse(readWriteRequest, TimeOut, CancellationToken.None);
                if (result.RequestInfo is NetMessageBase netMessage)
                {
                    return netMessage;
                }
                else
                {
                    return new Result<byte[]>();
                }
            }
            catch (Exception ex)
            {
                return new Result<byte[]>(ex.Message);
            }

        }
        public override Result<bool[]> ReadBool(string address, ushort length)
        {
            ReadWriteRequest readWriteRequest = new ReadWriteRequest()
            {
                ReadWriteType = ReadWriteType.ReadBools,
                DeviceAddress = new string[] { address },
                Length = new ushort[] { length },
            };
            try
            {
                try
                {
                    this.Connect(ConnectTimeOut);
                }
                catch (Exception)
                {
                    return new Result<bool[]>(ThingsGateway.Foundation.Resources.Resource.ConnectTimeOut);
                }
                var result = this.GetWaitingClient().SendThenResponse(readWriteRequest, TimeOut, CancellationToken.None);
                if (result.RequestInfo is NetMessageBase netMessage)
                {
                    if (netMessage.IsSuccess)
                        return Result.CreateSuccessResult(netMessage.Content.ByteToBoolArray(length));
                    else
                        return Result.CreateFailedResult<bool[]>(netMessage);

                }
                else
                {
                    return new Result<bool[]>();
                }
            }
            catch (Exception ex)
            {

                return new Result<bool[]>(ex.Message);
            }

        }
        public override Result<bool> ReadBool(string address)
        {
            ReadWriteRequest readWriteRequest = new ReadWriteRequest()
            {
                ReadWriteType = ReadWriteType.ReadBool,
                DeviceAddress = new string[] { address },
                Length = new ushort[] { 1 },
            };
            try
            {
                try
                {
                    this.Connect(ConnectTimeOut);
                }
                catch (Exception)
                {
                    return new Result<bool>(ThingsGateway.Foundation.Resources.Resource.ConnectTimeOut);
                }
                var result = this.GetWaitingClient().SendThenResponse(readWriteRequest, TimeOut, CancellationToken.None);
                if (result.RequestInfo is NetMessageBase netMessage)
                {
                    if (netMessage.IsSuccess)
                        return Result.CreateSuccessResult(netMessage.Content.ByteToBoolArray(1)[0]);
                    else
                        return Result.CreateFailedResult<bool>(netMessage);

                }
                else
                {
                    return new Result<bool>();
                }
            }
            catch (Exception ex)
            {
                return new Result<bool>(ex.Message);
            }
        }
        public virtual Result<byte[]> Send(byte[] value)
        {
            ReadWriteRequest readWriteRequest = new ReadWriteRequest()
            {
                ReadWriteType = ReadWriteType.Other,
                Bytes = value
            };
            try
            {
                try
                {
                    this.Connect(ConnectTimeOut);
                }
                catch (Exception)
                {
                    return new Result<byte[]>(ThingsGateway.Foundation.Resources.Resource.ConnectTimeOut);
                }
                var result = this.GetWaitingClient(WaitingOptions.AllAdapter).SendThenResponse(readWriteRequest, TimeOut, CancellationToken.None);
                if (result.RequestInfo is NetMessageBase netMessage)
                {
                    return netMessage;
                }
                else
                {

                    return new Result<byte[]>(Resource.UnknownError);
                }
            }
            catch (Exception ex)
            {
                return new Result<byte[]>(ex.Message);
            }

        }

        public override string ToString()
        {
            return RemoteIPHost.ToString();
        }
        public override Result Write(string address, byte[] value)
        {
            ReadWriteRequest readWriteRequest = new ReadWriteRequest()
            {
                ReadWriteType = ReadWriteType.WriteByte,
                DeviceAddress = new string[] { address },
                Bytes = value,
                Length = new ushort[] { (ushort)value.Length },

            };
            try
            {
                try
                {
                    this.Connect(ConnectTimeOut);
                }
                catch (Exception)
                {
                    return new Result<bool[]>(ThingsGateway.Foundation.Resources.Resource.ConnectTimeOut);
                }
                var result = this.GetWaitingClient(WaitingOptions.AllAdapter).SendThenResponse(readWriteRequest, TimeOut, CancellationToken.None);
                if (result.RequestInfo is NetMessageBase netMessage)
                {
                    return netMessage;
                }
                else
                {
                    return new Result(Resource.UnknownError);
                }
            }
            catch (Exception ex)
            {
                return new Result<bool[]>(ex.Message);

            }

        }

        public override Result Write(string address, bool[] value)
        {
            ReadWriteRequest readWriteRequest = new ReadWriteRequest()
            {
                ReadWriteType = ReadWriteType.WriteByte,
                DeviceAddress = new string[] { address },
                Bools = value,
                Length = new ushort[] { (ushort)value.Length },
            };
            try
            {
                try
                {
                    this.Connect(ConnectTimeOut);
                }
                catch (Exception)
                {
                    return new Result<bool[]>(ThingsGateway.Foundation.Resources.Resource.ConnectTimeOut);
                }
                var result = this.GetWaitingClient(WaitingOptions.AllAdapter).SendThenResponse(readWriteRequest, TimeOut, CancellationToken.None);
                if (result.RequestInfo is NetMessageBase netMessage)
                {
                    return netMessage;
                }
                else
                {
                    return new Result(Resource.UnknownError);
                }
            }
            catch (Exception ex)
            {
                return new Result<bool[]>(ex.Message);
            }

        }

        #endregion Public Methods
    }
}
